<template>
  <div class="app-container">
    <div class="search-box">
      <div class="search-hd">角色管理</div>
      <div class="search-bd">
        <el-button type="success" size="small" @click="formAdd=true">新增</el-button>
      </div>
    </div>
    <div class="main-box">
      <div class="main-hd">角色管理</div>
      <el-table
        v-loading="loading"
        :data="list"
        stripe
        max-height="500"
        fit
        highlight-current-row
      >
        <el-table-column  label="序号" width="95" align="center">
          <template slot-scope="scope">
            {{ scope.$index }}
          </template>
        </el-table-column>
        <el-table-column prop="roleName" label="角色名称" ></el-table-column>
          <el-table-column label="操作" width="420">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" plain @click="setPermission(scope.row)">权限分配</el-button>
            <el-button size="mini" type="primary" plain icon="el-icon-edit" @click="edit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" plain icon="el-icon-delete" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-box">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="searchField.page"
          :page-sizes="[10, 15, 20, 25, 30, 50]"
          :page-size="searchField.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
    <!--新增表单-->
    <el-dialog title="新增角色" :visible.sync="formAdd" width="500px" center :close-on-click-modal="false">
      <role-add @change-state="formAdd = $event" @reload="initTableData"></role-add>
    </el-dialog>
    <!--编辑表单-->
    <el-dialog title="角色编辑" :visible.sync="formEdit" width="500px" center :close-on-click-modal="false">
      <role-edit ref="editDialog" @change-state="formEdit = $event" @reload="initTableData" ></role-edit>
    </el-dialog>
    <!--权限设置表单-->
    <el-dialog title="权限设置" :visible.sync="permissionSet" width="500px" center :close-on-click-modal="false">
      <role-permission ref="permissionDialog" @change-state="permissionSet = $event" ></role-permission>
    </el-dialog>
  </div>
</template>

<script>
import { getProjectList } from '@/api/table'
import RoleAdd from './role-add'
import RoleEdit from './role-edit'
import RolePermission from './role-permission'
export default {
  data() {
    return {
      list: null,
      loading: true,
      total: 10,
      formAdd: false,
      formEdit: false,
      permissionSet: false,
      searchField: {
        page: 1,
        limit: 15
      }
    }
  },
  components: {
    RoleAdd,
    RoleEdit,
    RolePermission
  },
  created() {
    this.initTableData()
  },
  methods: {
    initTableData() {
      this.loading = true
      getProjectList().then(response => {
        this.list = response.data.items
        this.total = this.list.length
        this.loading = false
      })
      // this.loading = true
      // if (page) {
      //   this.searchField.page = page
      // } else {
      //   this.searchField.page = 1
      // }
      // this.$axios.get(`/org/role/get-list`, {
      //   params: this.searchField
      // })
      // .then(res => {
      //   this.loading = false
      //   if (res.data.code === 200 && res.data.obj) {
      //     this.tableData = res.data.obj
      //     this.total = res.data.other.total
      //   }
      // }).catch(() => {
      //   this.loading = false
      // })
    },
    setPermission (row) {
      this.permissionSet = true
      let _this = this
      this.$nextTick(() => {
        _this.$refs.permissionDialog.initDialogData(row.id)
      })
    },
    edit (row) {
      this.formEdit = true
      let _this = this
      this.$nextTick(() => {
        _this.$refs.editDialog.initDialogData(row.id)
      })
    },
    del (row) {
      // this.$axios.delete('/org/role/delete?id=' + row.id)
      // .then(res => {
      //   if (res.data.code === 200) {
      //     this.initTableData(1) // 重新加载列表数据
      //     this.$message.success(res.data.msg)
      //   }
      // }).catch(err => {
      //   this.$message.error(err)
      // })
    }, // 翻页
    handleSizeChange (val) {
      this.initTableData(val)
    }, // 切换mei
    handleCurrentChange (val) {
      this.searchField.limit = val
      this.initTableData()
    }
  }
}
</script>

<style lang="scss" scoped>
.redColor{
  color: red;
}
.search-box{
  margin-bottom: 15px;
  .search-hd{
    border-left: 2px solid #00a73c;
    padding: 10px;
    margin-bottom: 10px;
  }
  .search-bd{
    .el-button--small{
      margin-top: 3px;
    }
  }
}
.main-box{
  .main-hd{
    height: 40px;
    line-height: 40px;
    background: #00a73c;
    color: #fff;
    padding-left: 10px;
  }
  .page-box{
    padding: 10px;
    text-align: center;
  }
}
</style>
